package com.bhf.factory;

import java.awt.Color;
import java.awt.Image;
import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.swing.ImageIcon;

import com.bhf.entities.EnemyPlane;
import com.bhf.frame.MainFrame;
import com.bhf.utils.Util;

public class EnemyPlaneFactory {
	public static final Random rand = new Random();
	// private static int colorMapSize=0;
	private static List<Color> colorList = null;

	private static String img_dir = "img";
	/**
	 * 生产敌机
	 * 
	 * @return
	 */

	private static List<String> imgs = null;
	private static List<Image> imgDatas = null; // 简单的图片缓冲

	static {
		colorList = new ArrayList<Color>();
		// /有些颜色看不清,就定下来吧
		colorList.add(Color.BLUE);
		colorList.add(Color.CYAN);
		colorList.add(Color.DARK_GRAY);
		colorList.add(Color.GRAY);
		colorList.add(Color.GREEN);
		colorList.add(Color.MAGENTA);
		colorList.add(Color.ORANGE);
		colorList.add(Color.RED);
		colorList.add(Color.PINK);
		colorList.add(Color.YELLOW);
		colorList.add(Color.BLACK);

		imgs = getEnemyPlaneImg();
		if(imgs!=null && imgs.size()>0){
			imgDatas = initImgData(imgs);
		}
	}
	


	private static List<String> getEnemyPlaneImg() {
		String baseDir = System.getProperty("user.dir");
		File directory = new File(baseDir + File.separator + img_dir);
		String[] list = directory.list(new FilenameFilter() {
			public boolean accept(File dir, String name) {
				if (name.startsWith("ff") && name.endsWith(".png")) {
					return true;
				}
				return false;
			}
		});
		List<String> nl = new ArrayList<String>();
		for (String s : list) {
			nl.add(directory + File.separator + s);
			System.out.println(directory + File.separator + s);
		}
		return nl;
	}

	private static List<Image> initImgData(List<String> imgs2) {
		List<Image> l = new ArrayList<Image>();
		for (String s : imgs2) {
			Image imageIcon = new ImageIcon(s).getImage();
			if (imageIcon == null) {
				System.out.println("没找到"+s+"飞机图片");
			}
			l.add(imageIcon);
		}
		return l;

	}

	public static final EnemyPlane createEnemyPlane(MainFrame frame, int step) {
		int qu = 0;
		// EnemyPlane ep = new EnemyPlane(frame, randomColor(), randomImg(),
		// randomLage());
		EnemyPlane ep = new EnemyPlane(frame, randomColor(), randomImgData(), randomLage());
		// 敌机的位置
		int epLeft = rand.nextInt(MainFrame.WIDTH - EnemyPlane.PLANE_WIDTH);
		int epHeight = 0;
		ep.setLeft(epLeft);
		ep.setHeight(epHeight);
		// 下落的速度5-8
		int speed = rand.nextInt(15);
		while (speed < (2 + step * 0.4) || speed > (7 + step * 0.14)) { // /让速度更均匀
			speed = rand.nextInt(15);
			qu++;
			if (qu > 10) { // 防止死循环, 懒得算了.
				qu = 0;
				break;
			}
		}
		ep.setSpeed(speed);
		// 注册监听器
		ep.addEnemyPlaneListener(frame);
		return ep;
	}

	private static Image randomImgData() {
		long l = System.currentTimeMillis() % Integer.MAX_VALUE;
		int a = (int) l % imgs.size();
		return imgDatas.get(a);
	}

	private static double randomLage() {
		// int bili=rand.nextInt(5);
		// return (bili+6)/10 ;
		return 0.5;
	}

	private static String randomImg() {
		long l = System.currentTimeMillis() % Integer.MAX_VALUE;
		int a = (int) l % imgs.size();
		return imgs.get(a);
	}

	private static Color randomColor() {
		long l = System.currentTimeMillis() % Integer.MAX_VALUE;
		int a = (int) l % colorList.size();
		return colorList.get(a);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// new EnemyPlaneFactory().getEnemyPlaneImg();
	}

}
